package com.ykdl.member.kid.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.ykdl.member.kid.models.ArticleBean;
import com.ykdl.member.kid.models.CatalogArticleItem;
import com.ykdl.member.kid.models.CatalogBean;
import com.ykdl.member.kid.models.CatalogItemBean;
import com.ykdl.member.kid.models.CatalogTopicItem;
import com.ykdl.member.kid.models.TopicBean;

import net.wxxr.http.interfaces.IJsonParser;

public class HeatImageListBean implements Serializable, IJsonParser {
	private static final long serialVersionUID = -584559126385346897L;
	
	private ArrayList<TopicBean>  topics = null;
	private int next_cursor = 0;
	private int prev_cursor = 0;
	private int total_number = 0;
	
	private boolean isExpired = false;
	public boolean isExpired() {
		return isExpired;
	}
	public void setExpired(boolean isExpired) {
		this.isExpired = isExpired;
	}
	
	public ArrayList<TopicBean> getTopics() {
		if(topics == null){
			topics = new ArrayList<TopicBean>();
		}
		
		return topics;
	}
	public void setTopics(ArrayList<TopicBean> topics) {
		
		this.topics = topics;
	}
	public int getNext_cursor() {
		return next_cursor;
	}
	public void setNext_cursor(int next_cursor) {
		this.next_cursor = next_cursor;
	}



	public int getPrev_cursor() {
		return prev_cursor;
	}
	public void setPrev_cursor(int prev_cursor) {
		this.prev_cursor = prev_cursor;
	}
	public int getTotal_number() {
		return total_number;
	}


	public void setTotal_number(int total_number) {
		this.total_number = total_number;
	}


	@Override
	public IJsonParser parse(String json) throws Exception {
		Gson  gson = null;
		if(null != json){
			GsonBuilder b = new GsonBuilder();
			gson  = b.create();
			final HeatImageListBean  bean = new HeatImageListBean();
			if("null".equals(json))return bean;
			if("{}".equals(json) || "[]".equals(json))return bean;
			final JSONObject  object = new JSONObject(json);
			if(json.startsWith("{") && json.endsWith("}")
					&& object.has("error")
					&& object.getInt("error") == 20007){
				bean.setExpired(true);
				return bean;
			}
			
			final String  list = object.getString("list");
			if("null".equals(list) || list.equals("[]")) return bean;
			
			final JSONArray  topics_json_array = object.getJSONArray("list");
			if(topics_json_array != null && topics_json_array.length() > 0){
				for(int i = 0 ; i < topics_json_array.length() ; i++){
					if(null == topics_json_array.getString(i) || topics_json_array.getString(i).equals("{}")) continue;
					bean.getTopics().add(gson.fromJson(topics_json_array.getString(i), TopicBean.class));
				}
			}
			bean.setPrev_cursor(object.getInt("prev_cursor"));
			bean.setNext_cursor(object.getInt("next_cursor"));
			bean.setTotal_number(object.getInt("total_number"));
			
			return bean;
		}
		return null;
	}

}
